using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Configuration;


using Material.Entity;
using Material.Object;

namespace Material.App
{
    public partial class FrmHutangHDR : Form
    {
        private string connStr = ConfigurationManager.ConnectionStrings["dbconnection"].ToString();
        private TblBarang tblBarang;
        private TblBarangEntity tblBarangEntity;

        private  TblTransHutangHDR tblTransHutangHDR;
        private TblTransHutangHDREntity tblTransHutangHDREntity;

        TblTransHutangDTL tblTransHutangDTL;
        TblTransHutangDTLEntity tblTransHutangDTLEntity;

        public static int idTransHDR = 0;
        //private int idBarang = 0;
        private string stateInput = string.Empty;

        public FrmHutangHDR()
        {
            InitializeComponent();
            EnabledButtons(true,false,false,true,false,false);
            ClearTextbox();
            EnabledText(false);

            dataGridView1.RowsDefaultCellStyle.BackColor = Color.Bisque;
            dataGridView1.AlternatingRowsDefaultCellStyle.BackColor = Color.Beige;
            dataGridView1.CellBorderStyle = DataGridViewCellBorderStyle.None;

            dataGridView1.DefaultCellStyle.SelectionBackColor = Color.Green;
            dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Yellow;

            //dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
            //dataGridView1.Columns[1].DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;

            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            //dataGridView1.AllowUserToResizeColumns = false;
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        /// <summary>
        /// Kondisi Untuk Disable dan Enable Button
        /// </summary>
        /// <param name="blbtnNew"></param>
        /// <param name="blbtnCancel"></param>
        /// <param name="blbtnSave"></param>
        /// <param name="blbtnPrint"></param>
        private void EnabledButtons(bool blbtnNew, bool blbtnAddDetail, bool blbtnSave,
            bool btnFind,bool blbtnDelete,bool blbtnCancel)
        {
            btnNew.Enabled = blbtnNew;
            btnSave.Enabled = blbtnSave;
            btnAddDetail.Enabled = blbtnAddDetail;
            btnDelete.Enabled = blbtnSave;
            btnFindBarang.Enabled = btnFind;
            btnDelete.Enabled = blbtnDelete;
            btnCancel.Enabled = blbtnCancel;

        }

        private void EnabledText(bool state)
        {
            txtAlamat.Enabled = state;
            txtJumlah.Enabled = state;
            txtKeterangan.Enabled = state;
            txtNama.Enabled = state;
            txtNoHP.Enabled = state;
            //txtStatusHutang.Enabled = state;
            tglHutang.Enabled = state;
            tglLunas.Enabled = state;
            rbLunas.Enabled = state;
            rbBlmLunas.Enabled = state;
        }

        /// <summary>
        /// Clear Semua TextBox
        /// </summary>
        private void ClearTextbox()
        {
            txtNoHP.Text = string.Empty;
            txtJumlah.Text = string.Empty;
            txtNama.Text = string.Empty;
            txtAlamat.Text = string.Empty;
            txtKeterangan.Text = string.Empty;
        }


        private void btnFindBarang_Click(object sender, EventArgs e)
        {
            FrmFindPiutang frm = new FrmFindPiutang();
            frm.ShowDialog();

            //if (frm.idTransHDR.Trim().Length > 0)
            //{
            //    tblTransHutangHDREntity = new TblTransHutangHDREntity(connStr);
            //    tblTransHutangHDR = new TblTransHutangHDR();
            //    tblTransHutangHDR.IdTransHDR = Convert.ToInt32(frm.idTransHDR);
            //    tblTransHutangHDR = tblTransHutangHDREntity.GetDataByID(tblTransHutangHDR);

            //    txtAlamat.Text = tblTransHutangHDR.Alamat;
            //    txtJumlah.Text = tblTransHutangHDR.JumlahHutang.ToString();
            //    txtKeterangan.Text = tblTransHutangHDR.Keterangan;
            //    txtNama.Text = tblTransHutangHDR.Nama;
            //    txtNoHP.Text = tblTransHutangHDR.NoHP;
            //    tglHutang.Value = Convert.ToDateTime(tblTransHutangHDR.TglHutang);
            //    tglLunas.Value = Convert.ToDateTime(tblTransHutangHDR.TglLunas);
            //    idTransHDR = tblTransHutangHDR.IdTransHDR;
            //    BindGrid();

            //    EnabledButtons(false, true, true, false, true, true);
            //    EnabledText(true);
            //    stateInput = "Edit";
            //    idTransHDR = tblTransHutangHDR.IdTransHDR;
            //}

            


            //FrmFindBarang frm = new FrmFindBarang();
            //frm.ShowDialog();
            //if ( frm.idBarang.Trim().Length > 0)
            //{
            //    tblBarangEntity = new TblBarangEntity(connStr);
            //    tblBarang = new TblBarang();
            //    tblBarang.Id = Convert.ToInt32(frm.idBarang);
            //    idBarang = Convert.ToInt32(frm.idBarang);
            //    tblBarang = tblBarangEntity.GetDataByID(tblBarang);

            //    txtNama.Text = tblBarang.NamaBarang;
            //    txtNoHP.Text = tblBarang.TblKelompok.Kelompok;
            //    txtJumlah.Text = tblBarang.TblSatuan.Satuan;

            //    tblTransHutangHDR = new TblTransHutangHDR();
            //    tblTransHutangHDR.IdBarang = idBarang;
            //    tblTransHutangHDREntity = new TblTransHutangHDREntity(connStr);

            //    tblTransHutangHDR = tblTransHutangHDREntity.GetDataByIDBarang(tblTransHutangHDR);
            //    idTrans = tblTransHutangHDR.IdTrans;
            //    FrmPembBarangDTL.idTransHDR = idTrans;
            //    FrmPembBarangDTL.idBarang = idBarang;
            //    BindGrid();
            //}
        }

        private void btnAddDetail_Click(object sender, EventArgs e)
        {
            FrmHutangDTL frm = new FrmHutangDTL();
            FrmHutangDTL.stateInput = "Add";
            FrmHutangDTL.idTransHDR = idTransHDR;
            frm.ShowDialog();
            BindGrid();
            //if (txtNama.Text.Trim().Length != 0)
            //{
            //    FrmPembBarangDTL.stateInput = "Add";
            //    if (dataGridView1.Rows.Count == 0)
            //        FrmPembBarangDTL.isHeader = true;
            //    else
            //        FrmPembBarangDTL.isHeader = false;

            //    FrmPembBarangDTL frm = new FrmPembBarangDTL();
            //    frm.ShowDialog();
            //    BindGrid();
            //}
            //else
            //    MessageBox.Show("Barang harus dipilih terlebih dahulu", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

    

        private void btnNew_Click(object sender, EventArgs e)
        {
            
        }

        private void BindGrid()
        {
            tblTransHutangDTL = new TblTransHutangDTL();
            tblTransHutangDTLEntity = new TblTransHutangDTLEntity(connStr);

            tblTransHutangDTL.IdTransHDR = idTransHDR;
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = tblTransHutangDTLEntity.GetDataDSByIdHDR(idTransHDR.ToString()).Tables[0];
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            tblTransHutangDTL = new TblTransHutangDTL();
            tblTransHutangDTLEntity = new TblTransHutangDTLEntity(connStr);
            if ((e.RowIndex > -1) && (e.ColumnIndex > -1))
            {

                switch (e.ColumnIndex)
                {
                    case 3:
                        tblTransHutangDTL.IdTransDTL = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                        tblTransHutangDTL = tblTransHutangDTLEntity.GetDataByID(tblTransHutangDTL);
                        FrmHutangDTL.stateInput = "Edit";
                        FrmHutangDTL.idTransDTL = tblTransHutangDTL.IdTransDTL;
                        FrmHutangDTL frm = new FrmHutangDTL();
                        //FrmHutangDTL.isHeader = false;
                        frm.ShowDialog();
                        BindGrid();
                        break;

                    case 4:
                        DialogResult dr = MessageBox.Show("Data akan dihapus..", "Pesan", MessageBoxButtons.YesNo, MessageBoxIcon.Question,MessageBoxDefaultButton.Button2);
                        if (dr == DialogResult.Yes)
                        {
                            tblTransHutangDTL.IdTransDTL = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
                            int i = tblTransHutangDTLEntity.Delete(tblTransHutangDTL);
                        }
                        BindGrid();
                        
                        break;
                }
            }
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            if (txtNama.Text.Trim().Length != 0)
            {
                int i = 0;
                DialogResult dr = MessageBox.Show("Data akan dihapus..", "Pesan", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2);
                if (dr == DialogResult.Yes)
                {

                    tblTransHutangDTL = new TblTransHutangDTL();
                    tblTransHutangDTLEntity = new TblTransHutangDTLEntity(connStr);
                    tblTransHutangDTL.IdTransHDR = idTransHDR;
                    i = tblTransHutangDTLEntity.DeleteByIdTransHDR(tblTransHutangDTL);
                    if (i != -1)
                    {
                        tblTransHutangHDR = new TblTransHutangHDR();
                        tblTransHutangHDREntity = new TblTransHutangHDREntity(connStr);

                        tblTransHutangHDR.IdTransHDR = idTransHDR;
                        i = tblTransHutangHDREntity.Delete(tblTransHutangHDR);
                        //idTransHDR = 0;
                    }

                    if (i != -1)
                    {
                        MessageBox.Show("Data berhasil dihapus..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        ClearTextbox();
                        BindGrid();
                        EnabledText(false);
                        EnabledButtons(true, false, false, true, false, false);
                    }
                    else
                        MessageBox.Show("Data tidak berhasil dihapus..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Error);

                }
            }
            else
                MessageBox.Show("Barang harus dipilih terlebih dahulu", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);


            


        }

        private void btnNew_Click_1(object sender, EventArgs e)
        {
            EnabledButtons(false, false, true, false,false,true);
            EnabledText(true);
            ClearTextbox();
            stateInput = "Add";
            txtNama.Focus();
            idTransHDR = 0;
            BindGrid();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            EnabledText(false);
            EnabledButtons(true, false, false, true, false,false);
            ClearTextbox();
            idTransHDR = 0;
            BindGrid();
        }

        private void btnSave_Click_1(object sender, EventArgs e)
        {
            int i = 0;
            if (txtNama.Text.Trim().Length == 0)
            {
                MessageBox.Show("Kolom Nama Harus Diisi..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtNama.Focus();
                return;
            }
            else if (txtJumlah.Text.Trim().Length == 0)
            {
                MessageBox.Show("Kolom Jumlah Hutang Harus Diisi..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtJumlah.Focus();
                return;
            }

            else if (txtAlamat.Text.Trim().Length == 0)
            {
                MessageBox.Show("Kolom Alamat Harus Diisi..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                txtAlamat.Focus();
                return;
            }
            else
            {

                tblTransHutangHDR = new TblTransHutangHDR();
                tblTransHutangHDREntity = new TblTransHutangHDREntity(connStr);
                tblTransHutangHDR = tblTransHutangHDREntity.GetDataByMaxID();
                tblTransHutangHDR.IdTransHDR = tblTransHutangHDR.IdTransHDR;
                tblTransHutangHDR.Nama = txtNama.Text;
                tblTransHutangHDR.NoHP = txtNoHP.Text;
                tblTransHutangHDR.TglHutang = Convert.ToDateTime(tglHutang.Text);
                tblTransHutangHDR.TglLunas = Convert.ToDateTime(tglLunas.Text);
                tblTransHutangHDR.Alamat = txtAlamat.Text;
                tblTransHutangHDR.Keterangan = txtKeterangan.Text;
                tblTransHutangHDR.StatusLunas = 0;
                tblTransHutangHDR.JumlahHutang = Convert.ToDecimal(txtJumlah.Text);
                if (stateInput == "Add")
                {
                    tblTransHutangHDR.CreatedDate = DateTime.Now;
                    tblTransHutangHDR.CreatedBy = FrmLogin.UserID;
                    tblTransHutangHDR.ModifiedDate = DateTime.Now;
                    tblTransHutangHDR.ModifiedBy = FrmLogin.UserID;
                    i = tblTransHutangHDREntity.Insert(tblTransHutangHDR);

                }
                else
                {
                    tblTransHutangHDR.IdTransHDR = idTransHDR;
                    tblTransHutangHDR.ModifiedDate = DateTime.Now;
                    tblTransHutangHDR.ModifiedBy = FrmLogin.UserID;
                    i = tblTransHutangHDREntity.Update(tblTransHutangHDR);

                }


                if (i != -1)
                {
                    FrmHutangDTL.idTransHDR = tblTransHutangHDR.IdTransHDR;
                    idTransHDR = tblTransHutangHDR.IdTransHDR;
                    MessageBox.Show("Data berhasil disimpan..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    EnabledText(false);
                    EnabledButtons(true, true, false, true, true, false);
                }
                else
                    MessageBox.Show("Data tidak berhasil disimpan..", "Pesan", MessageBoxButtons.OK, MessageBoxIcon.Error);


            }
        }

        private void txtJumlah_KeyPress(object sender, KeyPressEventArgs e)
        {
            //Using Regular Expressions
            if (!System.Text.RegularExpressions.Regex.IsMatch(e.KeyChar.ToString(), "\\d+"))
                e.Handled = true;
        }
    }
}